<?php


namespace App\Http\Controllers\Database;


use Illuminate\Support\Facades\DB;

class FieldController extends BaseController
{

    // 获取数据表字段
    /**
     * 1、自己的模型的方法获取
     * 2、模型获取
     * 3、Db类获取
     */


    // 获取所有的数据表
    // http://www.shop.com/database/field/tables
    public function tables()
    {
        $tables = $this->getTables();
        $br     = chr(10).'<br/>';
        foreach($tables as $row){
            echo $row['tablename'].'：'.$row['table_comment'].'：'.$row['engine'];
            echo $br;
        }
    }

    // 选择表名称 查看
    // http://www.shop.com/database/field/index
    public function index()
    {
        echo $this->formHtml();
    }

    // 所有的字段信息（用于apipost的描述参数）
    // http://www.shop.com/database/field/apipost
    public function apipost()
    {
        $fields = $this->getFields();

        // 格式化
        //$fields = array_column($fields, null, 'field');
        $fields = array_column($fields, 'comment', 'field');

        echo json_encode($fields, JSON_UNESCAPED_UNICODE);
    }


    // 获取数据表的字段信息（用于模型，获取数据，等）
    // http://www.shop.com/database/field/fields
    public function fields()
    {
        $option = $this->optLogic();
        $table  = $option->getData('table', null, 'trim');
        if(empty($table)){
            $jump = '/database/field/index';
            echo '<h2>没有选定数据表，正在转跳中....</h2>';
            $this->showMsg('没有选定数据表，正在转跳中....');
            //sleep(3);
            //header('Location:/database/field/index');
            echo '<meta http-equiv="refresh" content="3;url='.$jump.'">';
            die;
        }


        // 获取字段
        $allField = $this->getFields('', $table);
        // 格式化
        //$fields = array_column($fields, null, 'field');
        $fields = array_column($allField, 'comment', 'field');
        $json   = json_encode($fields, JSON_UNESCAPED_UNICODE);
        $html   = $this->formHtml($table);

        // 循环操作
        $rule = $get = $show = $request = '';
        $br   = chr(10).'<br/>';
        foreach($allField as $field){
            $_type   = $field['type'];
            $_field  = $field['field'];
            $_func   = in_array($field['type'], ['int', 'tinyint']) ? 'intval' : 'trim';
            $show    .= "'{$_field}' => '{$field['comment']}',";
            $rule    .= "'{$_field}' => 'require',";
            $get     .= "'{$_field}' => \$opt->getData(\"{$_field}\", null,\"{$_func}\"),".$br;
            $request .= "\${$_field} = \$opt->getData(\"{$_field}\", null,\"{$_func}\");".$br;
        }

        $this->showMsg('操作数据表：'.$table, $html);
        $this->showMsg('字段信息，json', $json);
        $this->showMsg('字段信息，字段', '["'.implode('","', array_keys($fields)).'"]');
        $this->showMsg('字段信息，数组', var_export($fields, 1));
        $this->showMsg('字段信息，数组(同上)', '['.$show.']');
        $this->showMsg('验证规则', '['.$rule.']');
        $this->showMsg('获取数据', '['.$br.$get.']');
        $this->showMsg('控制器获取数据', $request);
    }


    // html选择表单选择表
    protected function formHtml($table = '')
    {
        $tables = $this->getTables();
        echo '<title>数据表的字段信息：完善模型与控制器</title>';
        $option = '<option value="">选择需要查看字段信息的数据表...</option>';
        foreach($tables as $row){
            $select = $row['tablename'] == $table ? 'selected' : '';
            $option .= '<option value="'.$row['tablename'].'" '.$select.'>'.$row['table_comment'].':'.$row['tablename'].'</option>';
        }

        $select = '<select name="table" style="font-size: 18px;height: 32px;">'.$option.'</select>';
        $submit = '<input type="submit" value="查看表字段信息">';

        return '<form action="/database/field/fields" method="post">'.$select.$submit.'</form>';
    }

}
